import {getRegistry} from '@/service';
// import { ITagListType,ITagDetailList} from '@/types';
import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';

export interface LoginModelState {
    registryList:object,
    // tagDetailList:ITagDetailList[]
}

export interface TagModelType {
  namespace: 'registry';
  state: LoginModelState;
  effects: {
    getRegistry:Effect
  };
  reducers: {
    save: Reducer<LoginModelState>;
  };
}

const RegistryModel: TagModelType = {
  // 数据模型，用来隔离数据防止相互影响
  namespace: 'registry',

  // 数据模型状态，相当于vuex的state
  state: {
    registryList: [],
  },

  // 异步操作，相当于vuex的action
  effects: {
    // 先简单记忆，*相当于async，yield相当于await
    *getRegistry({payload}, {call, put}){
      let result = yield getRegistry(payload);
      if (result.data){
          if(confirm('注册成功！是否前往登陆页面')){
              window.location.href="/login"
          }
        yield put({
          type: 'save',
          payload: {
            registryList: result.data,
          }
        })
      }
    }
  },
  // 同步操作，相当于vuex的mutation
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};

export default RegistryModel;
